package com.xhd.db;

import java.util.HashMap;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.exception.DatabaseException;
import com.xhd.server.ServerConfig;

public class DBControls {
	private String jm463 = null;
	  private boolean jm464 = false;
	  private String jm465 = null;
	  private boolean jm466 = true;
	  private HashMap jm467 = null;
	  private DBControl jm215 = null;
	  private HashMap jm468 = null;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	    
	      try
	      {
	         DBControls.jm10 = Class.forName("com.sinocc.systemdbmanager.DBControls");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	  }

	  public DBControls(String paramString)
	  {
	    this.jm463 = paramString;
	    if (ServerConfig.UseDBArea)
	    {
	      this.jm464 = true;
	      DBConfig localDBConfig = DBConfig.getConfig(this.jm463);
	      if (localDBConfig != null)
	      {
	        String str = localDBConfig.getArea();
	        this.jm464 = ((str != null) && (str.trim().length() > 0));
	      }
	      else
	      {
	        jm9.error("No found vault: " + this.jm463);
	      }
	    }
	    this.jm467 = new HashMap(2);
	  }

	  public DBControls(DBControl paramDBControl)
	  {
	    this.jm464 = false;
	    this.jm215 = paramDBControl;
	  }

	  public int getDBType()
	  {
	    if (this.jm215 != null)
	      return this.jm215.getDBType();
	    try
	    {
	      return getByArea(null).getDBType();
	    }
	    catch (Exception localException)
	    {
	    }
	    return 0;
	  }

	  protected DBControl getByObj(String paramString)
	    throws DatabaseException
	  {
	    if (this.jm215 != null)
	      return this.jm215;
	    if (this.jm464)
	    {
	      String str = XDBArea.getDBArea(this.jm463, paramString);
	      return getByArea(str);
	    }
	    return getByArea("");
	  }

	  public String getValueId()
	  {
	    return this.jm463;
	  }

	  protected DBControl getByArea(String paramString)
	    throws DatabaseException
	  {
	    if (this.jm215 != null)
	      return this.jm215;
	    DBControl localDBControl = null;
	    if (paramString == null)
	      if (this.jm465 != null)
	        paramString = this.jm465;
	      else
	        paramString = "";
	    localDBControl = (DBControl)this.jm467.get(paramString);
	    if (localDBControl == null)
	    {
	      DBConfig localDBConfig = DBConfig.getConfig(this.jm463, paramString);
	      if (localDBConfig == null)
	        throw new DatabaseException(30083, this.jm463, paramString);
	      if ((this.jm464) && (paramString.length() < 1) && (localDBConfig.getArea() != null))
	      {
	        paramString = localDBConfig.getArea();
	        localDBControl = (DBControl)this.jm467.get(paramString);
	        if (localDBControl != null)
	        {
	          if (this.jm465 == null)
	            this.jm465 = localDBConfig.getArea();
	          return localDBControl;
	        }
	      }
	      localDBControl = new DBControl(localDBConfig);
	      localDBControl.setIsLogCost(ServerConfig.IsLogSQLCost);
	      if (this.jm467.put(paramString, localDBControl) != null)
	        jm9.error("Replace connection but not free: " + localDBConfig.getId());
	      if ((this.jm468 != null) && (this.jm468.containsKey(paramString)))
	      {
	        Boolean localBoolean = (Boolean)this.jm468.get(paramString);
	        localDBControl.getConn().setTransation(localBoolean.booleanValue());
	      }
	      else
	      {
	        localDBControl.getConn().setTransation(this.jm466);
	      }
	      if (this.jm465 == null)
	        this.jm465 = paramString;
	    }
	    return localDBControl;
	  }

	  public void setTransation(String paramString, boolean paramBoolean)
	  {
	    if (paramString == null)
	    {
	      this.jm466 = paramBoolean;
	    }
	    else
	    {
	      if (this.jm468 == null)
	        this.jm468 = new HashMap();
	      this.jm468.put(paramString, Boolean.valueOf(paramBoolean));
	    }
	  }

	  public void commit()
	    throws DatabaseException
	  {
	    if (this.jm215 != null)
	    {
	      this.jm215.getConn().commit();
	      return;
	    }
	    Vector localVector = new Vector(this.jm467.values());
	    for (int i = 0; i < localVector.size(); ++i)
	    {
	      DBControl localDBControl = (DBControl)localVector.get(i);
	      if (!(localDBControl.isConnected()))
	        continue;
	      localDBControl.getConn().commit();
	    }
	    localVector = null;
	  }

	  public void commitContinue()
	    throws DatabaseException
	  {
	    if (this.jm215 != null)
	    {
	      this.jm215.getConn().commitContinue();
	      return;
	    }
	    Vector localVector = new Vector(this.jm467.values());
	    for (int i = 0; i < localVector.size(); ++i)
	    {
	      DBControl localDBControl = (DBControl)localVector.get(i);
	      if (!(localDBControl.isConnected()))
	        continue;
	      localDBControl.getConn().commitContinue();
	    }
	    localVector = null;
	  }

	  public void rollback()
	  {
	    if (this.jm215 != null)
	    {
	      try
	      {
	        this.jm215.getConn().rollback();
	      }
	      catch (Exception localException1)
	      {
	        jm9.error(localException1);
	      }
	      return;
	    }
	    Vector localVector = new Vector(this.jm467.values());
	    for (int i = 0; i < localVector.size(); ++i)
	    {
	      DBControl localDBControl = (DBControl)localVector.get(i);
	      if (!(localDBControl.isConnected()))
	        continue;
	      try
	      {
	        localDBControl.getConn().rollback();
	      }
	      catch (Exception localException2)
	      {
	        jm9.error(localException2);
	      }
	    }
	    localVector = null;
	  }

	  public void freeConnection()
	  {
	    if ((this.jm215 != null) && (this.jm215.isConnected()))
	      try
	      {
	        this.jm215.getConn().close();
	      }
	      catch (Exception localException1)
	      {
	        jm9.fatal(localException1);
	      }
	    if (this.jm467.size() <= 0)
	      return;
	    Vector localVector = new Vector(this.jm467.values());
	    for (int i = 0; i < localVector.size(); ++i)
	    {
	      DBControl localDBControl = (DBControl)localVector.get(i);
	      if (!(localDBControl.isConnected()))
	        continue;
	      try
	      {
	        localDBControl.getConn().close();
	      }
	      catch (Exception localException2)
	      {
	        jm9.fatal(localException2);
	      }
	    }
	    localVector = null;
	    this.jm467.clear();
	  }

	  public int getDBConnCount()
	  {
	    int i = 0;
	    if (this.jm215 != null)
	      return ((this.jm215.isConnected()) ? 1 : 0);
	    Vector localVector = new Vector(this.jm467.values());
	    for (int j = 0; j < localVector.size(); ++j)
	    {
	      DBControl localDBControl = (DBControl)localVector.get(j);
	      if (!(localDBControl.isConnected()))
	        continue;
	      ++i;
	    }
	    localVector.clear();
	    localVector = null;
	    return i;
	  }

	  public String getUseInfo(boolean paramBoolean)
	  {
	    long l = 0L;
	    int i = 0;
	    StringBuffer localStringBuffer = new StringBuffer(256);
	    if (this.jm215 != null)
	    {
	      if (this.jm215.isConnected())
	      {
	        if (paramBoolean)
	        {
	          localStringBuffer.append(this.jm215.getId());
	          localStringBuffer.append(':');
	          localStringBuffer.append(this.jm215.getLastSQL());
	          localStringBuffer.append("\r\n");
	        }
	        l += this.jm215.getTotalTime();
	        i += this.jm215.getCount();
	      }
	    }
	    else
	    {
	      Vector localVector = new Vector(this.jm467.values());
	      for (int j = 0; j < localVector.size(); ++j)
	      {
	        DBControl localDBControl = (DBControl)localVector.get(j);
	        if (!(localDBControl.isConnected()))
	          continue;
	        if (paramBoolean)
	        {
	          localStringBuffer.append(localDBControl.getId());
	          localStringBuffer.append(':');
	          localStringBuffer.append(localDBControl.getLastSQL());
	          localStringBuffer.append("\r\n");
	        }
	        l += localDBControl.getTotalTime();
	        i += localDBControl.getCount();
	      }
	      localVector = null;
	    }
	    localStringBuffer.append("COUNT=" + i);
	    localStringBuffer.append("; TOTALTIME=" + l);
	    return localStringBuffer.toString();
	  }

	  public int kill()
	  {
	    int i = 0;
	    if (this.jm215 != null)
	    {
	      if (this.jm215.isConnected())
	        try
	        {
	          i = this.jm215.getConn().kill();
	        }
	        catch (Exception localException1)
	        {
	          jm9.error(localException1);
	        }
	      return i;
	    }
	    Vector localVector = new Vector(this.jm467.values());
	    for (int j = 0; j < localVector.size(); ++j)
	    {
	      DBControl localDBControl = (DBControl)localVector.get(j);
	      if (localDBControl.isConnected())
	        try
	        {
	          i = localDBControl.getConn().kill();
	        }
	        catch (Exception localException2)
	        {
	          jm9.error(localException2);
	        }
	      if (i != 2)
	        break;
	    }
	    localVector = null;
	    return i;
	  }
}
